﻿using BusinessLogicLayer;
using IBusinessLogicLayer;
using Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Providers.Entities;

namespace One.Test.MVC.Controllers
{

    /// <summary>
    ///  登录的控制器
    /// </summary>
    public class LoginController : Controller
    {

        /// <summary>
        ///  退出登录
        /// </summary>
        /// <returns></returns>
        public int returnUser()
        {
            Session["LoginUser"] = null;
            return 1;
        }

        /// <summary>
        ///  登录视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        ///  登录判断
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <param name="validate"></param>
        /// <returns></returns>
        public ActionResult loginDo(string name, string pwd, string validate)
        {
            //验证验证码
            if (validate.ToUpper() == Session["CheckCode"].ToString().ToUpper())
            {
                #region
                //验证用户名和密码的值是否为空
                if (name != "" & pwd != "")
                {
                    #region
                    IUserBusinessLogicLayer users = new UserBusinessLogicLayer();
                    SqlParameter[]
                        Parameter = new SqlParameter[]{
                            new SqlParameter("@UserName",name),
                            new SqlParameter("@Password",pwd)
                           };
                    List<Users> UserList = users.Getlist<Users>(Parameter);
                    Session["LoginUser"] = UserList.FirstOrDefault();
                    Session["userId"] = UserList.FirstOrDefault().Id;
                    if (UserList != null)
                    {
                        int rolesid = UserList.First().RoleId;
                        if (rolesid == 1)
                        {
                            return Content("<script>alert('登录成功');location.href='/Admin/Index'</script>");
                        }
                        else if (rolesid == 2)
                        {
                            return Content("<script>alert('登录成功');location.href='/Teacher/Index'</script>");
                        }
                        else
                        {
                            return Content("<script>alert('登录成功');location.href='/Student/Index'</script>");
                        }
                    }
                    else
                    {
                        return Content("<script>alert('用户名或密码有误!');location.href='/Login/Login'</script>");
                    }
                    #endregion
                }
                else
                {
                    return Content("<script>alert('用户名密码不能为空!');location.href='/Login/Login'</script>");
                }
                #endregion
            }
            else
            {
                return Content("<script>alert('验证码有误!');location.href='/Login/Login'</script>");
            }
        }

    }
}
